package com.example.petstore.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

@Configuration//配置类
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject,"time", LocalDateTime.class,LocalDateTime.now());
//        this.strictUpdateFill(metaObject,"time",LocalDateTime.class,LocalDateTime.now());

        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("orderDate", LocalDateTime.now().toLocalDate(), metaObject);
        this.setFieldValByName("time", LocalDateTime.now(), metaObject);
//        this.setFieldValByName("time",new Date(), metaObject);
        this.setFieldValByName("status", "OK", metaObject);
        this.setFieldValByName("supplier", 1, metaObject);
        this.setFieldValByName("itemStatus", "P", metaObject);
        this.setFieldValByName("courier", "UPS", metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "time", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)

        this.setFieldValByName("time", LocalDateTime.now(), metaObject);
    }
}